<template>
<el-dialog :title="dialogTitle" :visible.sync="visible" append-to-body width="500px">
    <el-form :model="ruleForm" ref="ruleFormDialog" :rules="rules" label-width="80px" label-position="left">
        <source-picker-view ref="sourcePickerRef" type="source" v-model="ruleForm.source_id" placeholder="请选择渠道"></source-picker-view>
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
    </div>
</el-dialog>
</template>

<script>
import {
    appendSourceApi,
    saveSourceApi
} from '@/api/customer/source.js'
import sourcePickerView from "@/components/picker/source.vue";

const ruleForm = {
    member_id: "",
    source_id: "",
};

export default {
    inject: ["customerDetail"],
    components: {
        sourcePickerView
    },
    data() {
        return {
            dialogTitle: "",
            visible: false,
            type: "",
            ruleForm: Object.assign({}, ruleForm),
            sexOpts: [{
                id: 1,
                title: "男"
            }, {
                id: 2,
                title: "女"
            }, {
                id: 3,
                title: "未知"
            }],
            rules: {
                source_id: [{
                    required: true,
                    message: '请选择渠道'
                }]
            }
        }
    },
    methods: {
        showDialog(member_id, type) {
            this.dialogTitle = type == 'edit' ? '编辑渠道信息' : '添加重复渠道';
            this.type = type;
            this.visible = true;

            this.ruleForm.member_id = member_id;
            this.ruleForm.source_id = "";
            this.$nextTick(() => {
                this.$refs['sourcePickerRef'].clearCascader();
            })
        },
        closeDialog() {
            this.visible = false;
        },
        submit() {
            this.$refs['ruleFormDialog'].validate((valid) => {
                if (!valid) {
                    return false;
                }
                if (this.type == 'edit') {
                    saveSourceApi(this.ruleForm).then(res => {
                        this.closeDialog();
                        if (res.code == '200') {
                            this.customerDetail.getAllocateDetail();
                            this.$message.success(res.msg);
                        } else {
                            this.$message.error(res.msg);
                        }
                    })
                } else {
                    appendSourceApi(this.ruleForm).then(res => {
                        this.closeDialog();
                        if (res.code == '200') {
                            this.customerDetail.getAllocateDetail();
                            this.$message.success(res.msg);
                        } else {
                            this.$message.error(res.msg);
                        }
                    })
                }

            })
        }
    }
}
</script>

<style>

</style>
